package demo7;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.net.Socket;

@Component
@PropertySource("classpath:config.properties")
public class Client implements InitializingBean, DisposableBean {
    private Socket socket;
    private String host;
    private int port;


    public String getHost() {
        return host;
    }
    @Value("${client.host}")
    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    @Value("${client.port}")
    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        socket = new Socket(host, port);

    }

    @Override
    public void destroy() throws Exception {
        if (socket==null){
            socket.close();
        }
    }
}
